WorkerScript.onMessage = function(message) {
    doLogin(message);
}

function doLogin(message) {
    var xhr = new XMLHttpRequest;
    var url = message.g.serverOauth + "access_token?grant_type=password&client_id=" + message.g.clientId + "&client_secret=" + message.g.clientSecret +
        "&username=" + message.username + "&password=" + message.password;
    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        try {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                var response = JSON.parse(xhr.responseText);
                WorkerScript.sendMessage({ 'result': 'SUCCESS', 'token': response.access_token });
            }
        } catch (err) {
            WorkerScript.sendMessage({ 'result': 'FAILURE', 'message': err.message });
        }
    }
    xhr.send();
}
